/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/7/18
 * @Description: 
*/
#include "NetWorkCom.h"

NetWorkCom::NetWorkCom(tran::objectText &objectText)
{
    this->objectText = &objectText;
}

void NetWorkCom::generatedSign()
{
    QString MD5;
    QString temp = objectText->getAppid() + objectText->getQ().toUtf8() + objectText->getSalt() + objectText->getSecretKey();

    QByteArray str;
    str = QCryptographicHash::hash(temp.toUtf8(),QCryptographicHash::Md5);
    MD5.append(str.toHex());
    objectText->setSign(MD5);
}

QString NetWorkCom::begin()
{
    // 设置签名
    generatedSign();

    // 发送请求
    QString response = network();

    return response;
//    std::unordered_map<std::string, std::string> resultMap;
//    resultMap = returnText->getTrans_result();
//
//    return QString::fromStdString(resultMap["dst"]);
}
QString NetWorkCom::network()
{
    // 创建网络访问管理器和请求对象
    QNetworkAccessManager manager;
    QNetworkRequest request;

    QString url = this->objectText->getUrl();

    QUrlQuery query;
    qDebug() << QUrl::toPercentEncoding(this->objectText->getQ());

    // query.addQueryItem("q", QUrl::toPercentEncoding(this->objectText->getQ().toUtf8()));
    query.addQueryItem("from", this->objectText->getFrom());
    query.addQueryItem("to", this->objectText->getTo());
    query.addQueryItem("appid", this->objectText->getAppid());
    query.addQueryItem("salt", this->objectText->getSalt());
    query.addQueryItem("sign", this->objectText->getSign());

    QString urlWithQuery = url + "?q=" + QUrl::toPercentEncoding(this->objectText->getQ()) + '&' + query.toString();

    qDebug() << urlWithQuery;

    // 设置请求的URL
    request.setUrl(QUrl(urlWithQuery));

    // 发送Get请求
    QNetworkReply *reply = manager.get(request);
    QEventLoop loop;
    connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();

    // 处理返回的结果并返回
    QString response = reply->readAll();
    qDebug() << response;
    delete reply;

    QJsonDocument jsonResponse = QJsonDocument::fromJson(response.toUtf8());
    QJsonObject jsonObject = jsonResponse.object();
    QJsonValue translationValue = jsonObject.value("trans_result");

    if (translationValue.isArray())
    {
        QJsonArray translationArray = translationValue.toArray();
        if (!translationArray.isEmpty())
        {
            QJsonObject translationObject = translationArray.first().toObject();
            QString translation = translationObject.value("dst").toString();
            return translation;
        }
    }
    /*
     * 生成签名sign：
    Step1. 拼接字符串1：
    拼接appid=2015063000000001+q=apple+salt=1435660288+密钥=12345678得到
    字符串1：“2015063000000001apple143566028812345678”
    Step2. 计算签名：（对字符串1做MD5加密）
    sign=MD5(2015063000000001apple143566028812345678)，得到sign=f89f9594663708c1605f3d736d01d2d4

    拼接完整请求：
    http://api.fanyi.baidu.com/api/trans/vip/translate?
    q=apple&from=en&to=zh&appid=2015063000000001&salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4
    注：也可使用 POST 方式，如 POST 方式传送，Content-Type 请指定为：application/x-www-form-urlencoded
     * */
    return nullptr;
}
